package com.zl.user.controller;

import com.zl.basic.exception.BusinessException;
import com.zl.basic.util.AjaxResult;
import com.zl.basic.util.PageList;
import com.zl.user.domain.LoginInfo;
import com.zl.user.domain.User;
import com.zl.user.dto.UserDto;
import com.zl.user.query.UserQuery;
import com.zl.user.service.LoginInfoService;
import com.zl.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private LoginInfoService loginInfoService;

    /**
     * 查询一个对象的数据
     * */
    @GetMapping("/{id}")
    public User findOne(@PathVariable("id") Long id) {
        return userService.findById(id);
    }
    /**
     * 查询所有
     * */
    @GetMapping
    public List<User> findAll() {
        return userService.findAll();
    }
    /**
     * 分页查询
     * */
    @PostMapping
    public PageList<User> queryPage(@RequestBody UserQuery userQuery) {

        return userService.queryPage(userQuery);
    }

    /**
     * 删除操作
     * */
    @DeleteMapping("/{id}")
    public AjaxResult del(@PathVariable("id") Long id) {
        try {
            userService.remove(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "删除失败！！！");
        }
    }

    /**
     * 添加或者修改操作
     * */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody  User user) {
        try {
            if (user.getId()==null){
                userService.save(user);
            }else {
                userService.update(user);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败！！！");
        }
    }

    @PatchMapping
    public AjaxResult patchDel(@RequestBody List<Long> ids){
        try {
            userService.patchDel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "删除失败！！！");
        }
    }

    @PostMapping("/phone/register")
    public AjaxResult phoneRegister(@RequestBody UserDto userDto){
        try {
            userService.phoneRegister(userDto);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,"注册失败"+e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙,请稍后重试");
        }
    }


    @PostMapping("/email/register")
    public AjaxResult emailRegister(@RequestBody UserDto userDto){
        try {
            userService.emailRegister(userDto);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,"注册失败"+e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙,请稍后重试");
        }
    }

    @GetMapping("/emailActivation/{id}")
    public String emailActivation (@PathVariable("id") Long id){
        User user = userService.findById(id);
        user.setState(1);
        userService.update(user);
        LoginInfo loginInfo = loginInfoService.findById(user.getLogininfo_id());
        loginInfo.setDisable(true);
        loginInfoService.update(loginInfo);
        return  "<a href='http://127.0.0.1/login.html'>点击该链接登录</a>";
    }

}
